package ch.ethz.inf.shop.model;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import ch.ethz.inf.shop.Title;
import ch.ethz.inf.shop.model.simpleDatabase.Tuple;

public class Recipe {
	@Title(text = "RecipeId", isVisible = false)
	final private int recipeId;
	@Title(text = "Name", isVisible = true)
	final private String name;
	@Title(text = "Preparation Time", isVisible = true)
	final private Date preparationTime;
	@Title(text = "Instructions", isVisible = true)
	final private String instructions;

	@Title(text = "Total cost of ingredients", isVisible = true)
	final private BigDecimal costOfIngredients;

	@Title(text = "Items", isVisible = false)
	final private List<RecipeItem> items = new ArrayList<RecipeItem>();

	public int getRecipeId() {
		return recipeId;
	}

	public String getName() {
		return name;
	}

	public Date getPreparationTime() {
		return preparationTime;
	}

	public String getInstructions() {
		return instructions;
	}

	public BigDecimal getCostOfIngredients() {
		return costOfIngredients;
	}

	public List<RecipeItem> getItems() {
		return items;
	}

	public Recipe(int recipeId, String name, Date preparationTime, String instructions, BigDecimal costOfIngredients) {
		this.recipeId = recipeId;
		this.name = name;
		this.preparationTime = preparationTime;
		this.instructions = instructions;
		this.costOfIngredients = costOfIngredients;
	}

	public Recipe(final ResultSet rs) throws SQLException {
		this.recipeId = rs.getInt("recipe_id");
		this.name = rs.getString("name");
		this.preparationTime = rs.getTime("preparation_time");
		this.instructions = rs.getString("instructions");
		this.costOfIngredients = rs.getBigDecimal("cost_of_ingredients");
	}

	public Recipe(final Tuple tuple) throws ParseException {
		this.recipeId = tuple.getInt("recipe.recipe_id");
		this.name = tuple.getString("recipe.name");
		this.preparationTime = tuple.getTime("recipe.preparation_time");
		this.instructions = tuple.getString("recipe.instructions");
		this.costOfIngredients = tuple.getBigDecimal("recipe.cost_of_ingredients");
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((costOfIngredients == null) ? 0 : costOfIngredients.hashCode());
		result = prime * result + ((instructions == null) ? 0 : instructions.hashCode());
		result = prime * result + ((items == null) ? 0 : items.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((preparationTime == null) ? 0 : preparationTime.hashCode());
		result = prime * result + recipeId;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Recipe other = (Recipe) obj;
		if (costOfIngredients == null) {
			if (other.costOfIngredients != null)
				return false;
		} else if (!costOfIngredients.equals(other.costOfIngredients))
			return false;
		if (instructions == null) {
			if (other.instructions != null)
				return false;
		} else if (!instructions.equals(other.instructions))
			return false;
		if (items == null) {
			if (other.items != null)
				return false;
		} else if (!items.equals(other.items))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (preparationTime == null) {
			if (other.preparationTime != null)
				return false;
		} else if (!preparationTime.equals(other.preparationTime))
			return false;
		if (recipeId != other.recipeId)
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Recipe [recipeId=" + recipeId + ", name=" + name + ", preparationTime=" + preparationTime + ", instructions=" + instructions
				+ ", costOfIngredients=" + costOfIngredients + ", items=" + items + "]";
	}

}
